package hopital.orm;

import hopital.sql.SqlObject;
import hopital.sql.SqlQuery;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Bloc extends SqlObject{
    private String _nom;

    @Override
    public String toString() {
        return _nom ;
    }
    
    
    
    public Bloc() {}

    public Bloc(String _nom) {
        this._nom = _nom;
    }
    
    public Bloc(ResultSet _rs) throws SQLException {
        this._id=_rs.getInt("c_id");
        this._nom=_rs.getString("c_nom");
    }

    @Override
    public SqlQuery insert() {
        return new SqlQuery("INSERT INTO t_blocs (c_nom) VALUES(?);", _nom);
    }

    @Override
    public SqlQuery update() {
        return new SqlQuery("UPDATE t_blocs SET c_nom=?WHERE c_id=?;", _nom,_id);
    }

    @Override
    public SqlQuery delete() {
        return new SqlQuery("DELETE FROM t_blocs WHERE c_id=?;", _id);
    }

    @Override
    public SqlQuery select() {
        if(_id!=-1){
            return new SqlQuery("SELECT * FROM t_blocs WHERE c_id=?;", _id);
        }else{
            return new SqlQuery("SELECT * FROM t_blocs;");
        }
    }

    @Override
    public List<Object> getTable(ResultSet _rs) throws Exception {
        List<Object> list=new ArrayList<>();
        while (_rs.next()) {            
            list.add(new Bloc(_rs));
        }
        return list;
    }
}